package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuManageServiceImpl implements SpuManageService {
    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;
    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuPosterMapper spuPosterMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    //spu分页列表
    @Override
    public IPage<SpuInfo> getSpuInfoPage(IPage<SpuInfo> iPage, SpuInfo spuInfo) {
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        //根据分类id查询
        if (spuInfo.getCategory3Id() != null) {
           queryWrapper.eq(SpuInfo::getCategory3Id,spuInfo.getCategory3Id());
        }
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        iPage = spuInfoMapper.selectPage(iPage,queryWrapper);
        return iPage;
    }

    //获取销售属性数据
    @Override
    public List<BaseSaleAttr> BaseSaleAttr() {
        return baseSaleAttrMapper.selectList(null);
    }

    //保存商品spu信息
    /**
     * 保存商品SPU信息
     * 1.保存商品基本信息到spu_info表
     * 2.保存商品图片到spu_image表 关联到商品spu
     * 3.保存商品海报图片到spu_poster表 关联到商品spu
     * 4.保存商品Spu对应的销售属性名称到spu_sale_attr表中 。。
     * 5.保存商品Spu对应销售属性值表到spu_sale_attr_value表 。。
     *
     * @param spuInfo
     */
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1 保存商品基本信息到spu_info表
        spuInfoMapper.insert(spuInfo);
        //2 保存商品图片到spu_image表 关联到商品spu
        if (!CollectionUtils.isEmpty(spuInfo.getSpuImageList())) {
            for (SpuImage spuImage : spuInfo.getSpuImageList()) {
                //将图片关联到商品spu
                spuImage.setSpuId(spuInfo.getId());
                spuImageMapper.insert(spuImage);
            }
        }
        //3 保存商品海报图片到spu_poster表 关联到商品spu
        if (!CollectionUtils.isEmpty(spuInfo.getSpuPosterList())) {
            for (SpuPoster spuPoster : spuInfo.getSpuPosterList()) {
                spuPoster.setSpuId(spuInfo.getId());
                spuPosterMapper.insert(spuPoster);
            }
        }
        //4 保存商品spu对应的销售属性名称到spu_sale_attr表中 关联到商品spu
        if (!CollectionUtils.isEmpty(spuInfo.getSpuSaleAttrList())) {
            for (SpuSaleAttr spuSaleAttr : spuInfo.getSpuSaleAttrList()) {
                spuSaleAttr.setSpuId(spuInfo.getId());
                spuSaleAttrMapper.insert(spuSaleAttr);
        //5 保存商品Spu对应销售属性值表到spu_sale_attr_value表 关联到商品spu
                //5.1 处理当前销售属性对应的销售属性值集合
                if (!CollectionUtils.isEmpty(spuSaleAttr.getSpuSaleAttrValueList())) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttr.getSpuSaleAttrValueList()) {
                        //手动关联商品spuId
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        //设置当前销售属性值对应属性名称
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    }
                }
            }
        }
    }
}
